import { message } from "ant-design-vue";
import { defineStore } from "pinia";
let timer: any = null;
export const useEventStore = defineStore("event", {
  state: () => ({
    startMeasurement: 0, // 0:未开始，1：开始测量，2：结束测量
    operate: {
      x: 0,
      y: 0
    },
    operate2: {
      x: 0,
      y: 0
    },
    operate3: {
      x: 36,
      y: 0
    },
    operate4: 0,
    chooseType: '',
    chooseSure: false,
    chooseFinifsh: false,
    resetParameters: false,
    savePop: false,
    saveContent: ""
  }),
  getters: {
    getStartMeasurement(state: any) {
      return state.startMeasurement;
    },
    getOperate(state: any) {
      return state.operate;
    },
    getResetParameters(state: any) {
      return state.resetParameters;
    }
  },
  actions: {
    setStartMeasurement(visible: number) {
      this.startMeasurement = visible;
    },
    setChooseSure(visible: boolean) {
      this.chooseSure = visible;
    },
    setSavePoup(visible: boolean) {
      this.savePop = visible;
    },
    setSaveContent(content: string) {
      this.saveContent = content
    },
    setChooseType(direction: string) {
      this.chooseType = direction
      if (this.chooseType == '') {
        this.operate2 = {
          x: 0,
          y: 0
        }
        this.operate3 = {
          x: 36,
          y: 0
        }
        this.chooseFinifsh = false
      }
    },
    setResetParameters(visible: boolean) {
      this.resetParameters = visible;
      this.startMeasurement = 0
      //this.operate = { x: 0, y: 0 };
    },
    setOperate(direction: string) {
      if(this.chooseFinifsh){
        return
      }
      switch (direction) {
        case "left":
          this.operate.x += -1;
          if (this.operate.x <= 0) {
            this.operate.x = 0;
          }
          break;
        case "right":
          this.operate.x += 1;
          if (this.operate.x >= 48) {
            this.operate.x = 48;
          }
          break;
        case "top":
          this.operate.y += -1;
          if (this.operate.y <= 0) {
            this.operate.y = 0;
          }
          break;
        case "bottom":
          this.operate.y += 1;
          if (this.operate.y >= 48) {
            this.operate.y = 48;
          }
          break;
      }
      if (this.operate.x >20 && this.operate.y >20 && this.operate.x <28 && this.operate.y <28) {
        this.chooseFinifsh = true
      } else {
        this.chooseFinifsh = false
      }
    },
    setOperate2(direction: string) {
      if (this.chooseType == '') {
        console.log(0, '走这里了')
        message.warn('当前实验未进行到这一步，操作无法执行，请查看实验流程提示。')
        return
      }
      if (this.chooseFinifsh) {
        return
      }
      switch (direction) {
        case "left":
          this.operate2.x += -1;
          if (this.operate2.x <= 0) {
            this.operate2.x = 0;
          }
          break;
        case "right":
          this.operate2.x += 1;
          if (this.operate2.x >= 48) {
            this.operate2.x = 48;
          }
          break;
        case "top":
          this.operate2.y += -1;
          if (this.operate2.y <= 0) {
            this.operate2.y = 0;
          }
          break;
        case "bottom":
          this.operate2.y += 1;
          if (this.operate2.y >= 48) {
            this.operate2.y = 48;
          }
          break;
      }
      if (this.operate2.x == this.operate3.x && this.operate2.y == this.operate3.y) {
        this.chooseFinifsh = true
      } else {
        this.chooseFinifsh = false
      }
    },
    setOperate3(direction: string) {
      if (this.chooseType == '') {
        console.log(113, '走这里了')
        message.warn('当前实验未进行到这一步，操作无法执行，请查看实验流程提示。')
        return
      }
      if (this.chooseFinifsh) {
        // message.success('恭喜你，已完成本步操作。')
        return
      }
      switch (direction) {
        case "left":
          this.operate3.x += -1;
          if (this.operate3.x <= 0) {
            this.operate3.x = 0;
          }
          break;
        case "right":
          this.operate3.x += 1;
          if (this.operate3.x >= 48) {
            this.operate3.x = 48;
          }
          break;
        case "top":
          this.operate3.y += -1;
          if (this.operate3.y <= 0) {
            this.operate3.y = 0;
          }
          break;
        case "bottom":
          this.operate3.y += 1;
          if (this.operate3.y >= 48) {
            this.operate3.y = 48;
          }
          break;
      }
      if (this.operate2.x == this.operate3.x && this.operate2.y == this.operate3.y) {
        this.chooseFinifsh = true
      } else {
        this.chooseFinifsh = false
      }
    },
    setOperate4(direction: string) {
      if (direction == 'left') {
        if (this.operate4 == 100 || this.operate4 == -100) {
          if (timer) {
            clearInterval(timer)
          }
          return
        }

        if (timer) {
          clearInterval(timer)
          timer = setInterval(() => {
            if (this.operate4 == -100) {
              clearInterval(timer)
              return
            }
            this.operate4--
          }, 1000)
        } else {
          timer = setInterval(() => {
            if (this.operate4 == -100) {
              clearInterval(timer)
              return
            }
            this.operate4--
          }, 1000)
        }
      } else {
        if (this.operate4 == 100 || this.operate4 == -100) return
        if (timer) {
          clearInterval(timer)
          timer = setInterval(() => {
            if (this.operate4 == 100) {
              clearInterval(timer)
              return
            }
            this.operate4++
          }, 1000)
        } else {
          timer = setInterval(() => {
            if (this.operate4 == 100) {
              clearInterval(timer)
              return
            }
            this.operate4++
          }, 1000)
        }
      }
    },
    setResetState() {
      if (timer) {
        clearInterval(timer)
      }
      this.startMeasurement = 0;
      this.operate = { x: 0, y: 0 };
      this.operate2 = { x: 0, y: 0 };
      this.operate3 = { x: 0, y: 0 };
      this.operate4 = 0;
      this.chooseType = ''
      this.chooseSure = false;
      this.chooseFinifsh = false;
      this.resetParameters = false;
      this.savePop = false;
      this.saveContent = ''



    }
  },
});
